<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Origin:localhost:8080');
header('Content-Type:application/json; charset=utf-8');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS');
defined('BASEPATH') or exit('No direct script access allowed');
require 'JWT.php';
class BaseApiController extends CI_Controller
{
    public $jwt;
    public $TOKEN;
 
    private $response=array(
      'code'=>0,
      'msg'=>'success'
    );
	
    public function __construct()
    {
        parent::__construct();
        $this->jwt=new JWT();
        $headers=getallheaders();
        if (array_key_exists("Token", $headers)) {
            $this->TOKEN=$headers["Token"].'';
        } else {
            $this->TOKEN='';
        }
    }
	
	
	
    
	
	public function getFields($fields){
		//先检测请求方法
		$notexist=[];
		$data=[];
		foreach ($fields as $k => $v) {
			if($v[1]=="required"){
				if($this->input->post($v[0])==NULL){
					array_push($notexist,$v[2]);
				}else{
					$data[$v[0]]=$this->input->post($v[0]);
				}
			}else{
				if($this->input->post($v[0])==NULL){
					$data[$v[0]]=$v[3];
				}else{
					$data[$v[0]]=$this->input->post($v[0]);
				}
			}
		}
		if(count($notexist)>0){
			$this->WRITE("code",1001);
			$this->WRITE("msg","缺少必要参数:".implode(",",$notexist));
			$this->RESPONSE();
			exit();
		}
		return $data;
	}
	
	public function checkFields($key){
		
	}
	
	
	public function getField($field){
		$key=$field[0];
		$required=$field[1];
		$msg=$field[2];
		$default=$field[3];
		
		if ($this->input->get($key, true)!='') {
		    return $this->input->get($key, true);
		} elseif ($this->input->post($key, true)!='') {
		    return $this->input->post($key, true);
		}
		return $default;
		
		
	}
	
	
  
 
    public function getKey($value, $default)
    {
        if ($this->input->get($value, true)!='') {
            return $this->input->get($value, true);
        } elseif ($this->input->post($value, true)!='') {
            return $this->input->post($value, true);
        }
        return $default;
    }
    
    /*根据keys获取参数值*/
    public function getKeys($keys)
    {
		$keys=explode(",",$keys);
        $urldata=array();
		foreach ($keys as $key => $value) {
            if ($this->input->get($value, true)!='') {
                $urldata[$value]=$this->input->get($value, true);
            } else {
                if ($this->input->post($value, true)!='') {
                    $urldata[$value]=$this->input->post($value, true);
                }
            }
        }
        return $urldata;
    }
    
    public function checkKeys($requires)
    {
        $data=$this->getKeys($requires);
		$requires=explode(",",$requires);
        $notexist=array();
        foreach ($requires as $rkey=>$rvalue) {
            if (!isset($data[$rvalue])) {
                $notexist[]=$rvalue;
            }
        }
        if (count($notexist)>0) {
            $this->response['code']=1001;
            $this->response['msg']="缺少必要参数:".implode(",", $notexist);
            echo json_encode($this->response, JSON_UNESCAPED_UNICODE);
            return false;
        } else {
            return true;
        }
    }
    
    //写入某个字段值，没有则会自动添加
    public function WRITE($key, $value)
    {
        if (array_key_exists($key, $this->response)) {
            $this->response[$key]=$value;
        } else {
            $this->response[$key]=$value;
        }
    }
    
    public function RESPONSE()
    {
        echo json_encode($this->response, JSON_UNESCAPED_UNICODE);
    }
    
    
    /***********************JWT相关操作*******************************/
    public function createToken($data)
    {
        //JWT生成算法
        $this->jwt->setClaim($data);
        return $this->jwt->createToken();
    }
    
	public function decodeToken($token){
		$vtoken=$this->jwt->verifyToken($token);
		if (!$vtoken) {
			header('Content-Type:text/html; charset=utf-8');
			$this->WRITE("code",401);
			$this->WRITE("msg","登录状态或无权访问");
			$this->RESPONSE();
			return false;
		}
		return $vtoken['claim'];
	}
	
    public function verifyToken()
    {
 		$token=$this->TOKEN;
 		if(empty($token)){
			header('Content-Type:text/html; charset=utf-8');
			$this->WRITE("code",401);
			$this->WRITE("msg","登录状态或无权访问");
			$this->RESPONSE();
			return false;
			
		}else{
			$vtoken=$this->jwt->verifyToken($token);
			if (!$vtoken) {
				header('Content-Type:text/html; charset=utf-8');
				$this->WRITE("code",401);
				$this->WRITE("msg","登录状态或无权访问");
				$this->RESPONSE();
				return false;
			}
		}
		return $vtoken['claim'];
    }
 
}
